import sys


class ListNode:
    def __init__(self, name, id_, cost, child_list):
        self.name = name
        self.id_ = id_
        self.cost = cost
        self.nextops = child_list  # List[int]


if __name__ == "__main__":
    # n = int(sys.stdin.readline().strip())  # n <= 100
    # ops = []
    # for i in range(n):
    #     line = sys.stdin.readline().strip().split(' ')
    #     ops.append(ListNode(name=line[0], id_=i, cost=int(line[1]), child_list=list(map(int, line[2:]))))
    
    n = 6
    # lines = ["softmax 10  1  2", "relu 5", "conv1 1 3", "softmax  2"]
    lines = ["softmax 2  2 3 4", "zz 1", "relu 1 1 5", "softmax 2 5", "conv1 3 1", "w 3"]
    ops = []
    for i, line in enumerate(lines):
        line = line.split()
        ops.append(ListNode(name=line[0], id_=i, cost=int(line[1]), child_list=list(map(int, line[2:]))))
    
    
    # Calculate Time
    time = 0
    queue = list()
    queue.append([0, 0])  # 输入的索引，cost now
    pids = [0]
    for pair in queue:
        node = ops[pair[0]]
        cost = pair[1] + node.cost
        if not node.nextops:
            time = max(time, cost)
            continue
        # pids = [p[0] for p in queue]
        for child_id in node.nextops:
            if child_id in pids:
                i = pids.index(child_id)
                queue[i][1] = max(queue[i][1], cost)
            else:
                queue.append([child_id, cost])
                pids.append(child_id)
    print(time)
